package com.eascs.admin.o2o.repayment.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.o2o.repayment.dao.O2oRepaymentDao;
import com.eascs.admin.o2o.repayment.vo.O2oRepaymentVo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;

/**
 * @author CXW
 * @time 17/05/02 PM 15:56
 */
@Service
public class O2oRepaymentService {

	@Autowired
	O2oRepaymentDao o2oRepaymentDao;
	
	/**
	 * @Description:
	 *               <p>
	 *               分页查询还款计划
	 *               </p>
	 * @author CXW
	 * @date 2017年5月2日 下午4:09:52
	 * @version V1.0
	 */
	public PageResult<Object[]> findDataWithPage(O2oRepaymentVo vo){
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getPageIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT ol.loanapply_no,opp.bill_code,opr.province_name,opp.name,ol.operator_name,opp.periods,opp.payments_date ");
		querySql.append(" ,opp.residual_principal,opp.pay_principal,opp.pay_interest,ol.repay_mentway ");
		querySql.append(" ,ba.loan_date,ol.end_bank_date,ol.bank_loan_name,(case when ol.bank_amount is null then ol.ea_mount ");
		querySql.append(" when ol.ea_mount is null then ol.loana_mount else ol.bank_amount end)/10000 as fkamount ");
		querySql.append(" FROM o2o_loanapply ol RIGHT JOIN o2o_payments_plan opp on ol.bill_code=opp.bill_code ");
		querySql.append(" LEFT JOIN o2o_platform_regional opr on ol.ltd = opr.platform_id ");
		querySql.append(" LEFT JOIN o2o_bank_loan_data ba on ba.bill_code = ol.bill_code ");// o2o_bank_loan_data--ba.loan_date放款日期
		querySql.append(" where /**DATA_PERMISSION(ol.LTD)**/ opp.is_del='N' and ol.loan_type in ('BK','BP','BP_JX','SG','YC','SC') ");
		if(!StringUtils.isEmpty(vo.getBillCode())){
			querySql.append(" and opp.bill_code like ? ");
			sqlPageQuery.addLikeParam(vo.getBillCode());
		}
		if(!StringUtils.isEmpty(vo.getLtd())){
			querySql.append(" and opr.province_code = ? ");
			sqlPageQuery.addParam(vo.getLtd());
		}
		if(!StringUtils.isEmpty(vo.getUserName())){
			querySql.append(" and opp.name like ? ");
			sqlPageQuery.addLikeParam(vo.getUserName());
		}
		if(!StringUtils.isEmpty(vo.getJhStartTime())){
			querySql.append(" and opp.payments_date >= ? ");
			sqlPageQuery.addParam(vo.getJhStartTime());
		}
		if(!StringUtils.isEmpty(vo.getJhEndTime())){
			querySql.append(" and opp.payments_date <= ? ");
			sqlPageQuery.addParam(vo.getJhEndTime());
		}
		if(!StringUtils.isEmpty(vo.getFkStartTime())){
			querySql.append(" and ol.payment_time >= ? ");
			sqlPageQuery.addParam(vo.getFkStartTime());
		}
		if(!StringUtils.isEmpty(vo.getFkEndTime())){
			querySql.append(" and ol.payment_time <= ? ");
			sqlPageQuery.addParam(vo.getFkEndTime());
		}
		if(!StringUtils.isEmpty(vo.getDqStartTime())){
			querySql.append(" and ol.end_bank_date >= ? ");
			sqlPageQuery.addParam(vo.getDqStartTime());
		}
		if(!StringUtils.isEmpty(vo.getDqEndTime())){
			querySql.append(" and ol.end_bank_date <= ? ");
			sqlPageQuery.addParam(vo.getDqEndTime());
		}
		if(!StringUtils.isEmpty(vo.getBankName())){
			querySql.append(" and ol.bank_loan_name = ? ");
			sqlPageQuery.addParam(vo.getBankName());
		}
		
		if (StringUtil.hasText(vo.getOperatorName())) {
			querySql.append(" and ol.operator_name like ?");
			sqlPageQuery.addLikeParam(vo.getOperatorName());
		}
		querySql.append(" GROUP BY opp.payments_id ");
		querySql.append(" ORDER BY opp.payments_date desc,ol.ltd_name asc,ol.payment_time asc ");
		sqlPageQuery.setQuerySql(querySql.toString());
		return o2oRepaymentDao.findDataWithPage(sqlPageQuery);
	}
}
